package com.danco.training.zaneuskaya.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.danco.training.zaneuskaya.api.UserService;
import com.danco.training.zaneuskaya.connection.HibernateSessionUtil;
import com.danco.training.zaneuskaya.connection.dao.AuditorDao;
import com.danco.training.zaneuskaya.connection.dao.UserDao;
import com.danco.training.zaneuskaya.connection.dao.impl.AuditorDaoImpl;
import com.danco.training.zaneuskaya.connection.dao.impl.UserDaoImpl;
import com.danco.training.zaneuskaya.cswworker.CSVReader;
import com.danco.training.zaneuskaya.cswworker.CSVSaver;
import com.danco.training.zaneuskaya.cswworker.Separator;
import com.danco.training.zaneuskaya.domain.Auditor;
import com.danco.training.zaneuskaya.domain.User;

public class UserServiceImpl implements UserService{
	
	
	private static final Logger logger = Logger
			.getLogger(UserServiceImpl.class);
	
	private UserDao userDao = new UserDaoImpl();
	private AuditorDao auditorDao = new AuditorDaoImpl();

	@Override
	public User getById(Integer id) {
		User user = null;
		Session session = null;
		Transaction transaction = null;
		
		try {
			session = HibernateSessionUtil.getSessionFactory().getCurrentSession();
			transaction = session.beginTransaction();
			user = userDao.read(session, id);
			transaction.commit();
			
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
			transaction.rollback();
		}
		return user;
	}

	@Override
	public List<User> getList() {
		Session session = null;
		Transaction transaction = null;
		List<User> res = new ArrayList<User>();
		try {
			session = HibernateSessionUtil.getSessionFactory().getCurrentSession();
			transaction = session.beginTransaction();
			res.addAll(userDao.readAll(session));
			transaction.commit();
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
			transaction.rollback();
		}
		logger.trace("User list");
		return res;

	}
	@Override
	public void addEntity(User e) {
		Transaction transaction = null;
		Session session = null;
		try {
			session = HibernateSessionUtil.getSessionFactory().getCurrentSession();
			transaction = session.beginTransaction();
			userDao.create(session, e);
			transaction.commit();
		} catch (HibernateException e1) {
			logger.error(e1.getMessage(), e1);
			transaction.rollback();
		}

	}

	@Override
	public void updateEntity(User e) {
		Session session = null;
		Transaction transaction = null;
		try {
			session = HibernateSessionUtil.getSessionFactory().getCurrentSession();
			transaction = session.beginTransaction();
			userDao.update(session, e);
			transaction.commit();
		} catch (HibernateException e1) {
			logger.error(e1.getMessage(), e1);
			transaction.rollback();
		}
	}

	@Override
	public void removeEntityById(Integer id) {
		
    Transaction transaction = null;
	Session session = null;
	try {
		session = HibernateSessionUtil.getSessionFactory().getCurrentSession();
		transaction = session.beginTransaction();
		User user = userDao.read(session, id);
		userDao.delete(session, user);

		transaction.commit();

	} catch (HibernateException e) {
		transaction.rollback();
		logger.error(e.getMessage(), e);
	}

}

	@Override
	public void saveEntityListToFile(String file) {
		ArrayList<String> res = new ArrayList<String>();
		for (User u : getList()) {
			res.add(entityDetails(u));
		}
		try {
			CSVSaver.saveListToFile(file, res);
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		}
	}

	@Override
	public List<User> readEntityList(String file) {
		Session session = null;
		ArrayList<User> res = new ArrayList<User>();
		for (int i = 0; i < CSVReader.readFile(file).length; i++) {
			String user[] = CSVReader.readFile(file)[i]
					.split(Separator.SEPARATOR.getS());
			try {
				session = HibernateSessionUtil.getSessionFactory().getCurrentSession();
				User u = new User(Integer.parseInt(user[0]), user[1], user[2], user[3]);
				res.add(u);
			} catch (NumberFormatException e) {
				logger.error(e.getMessage(), e);
			} catch (HibernateException e) {
				logger.error(e.getMessage(), e);
			}

		}
		return res;
	}

	@Override
	public String entityDetails(User e) {
		
		return e.getName();
	
	}

	@Override
	public User loadUser(String login, String password) {
		User user = null;
		Session session = null;
		Transaction transaction = null;
		
		Auditor auditor = new Auditor(new Date(), login, "LOGIN");
		
		try {
			session = HibernateSessionUtil.getSessionFactory().getCurrentSession();
			transaction = session.beginTransaction();
			user = userDao.login(session, login, password);
			auditorDao.create(session, auditor);
			transaction.commit();
			
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
			transaction.rollback();
		}
		return user;
	}

	public void logoutUser(Integer userId) {
		User user = null;
		Session session = null;
		Transaction transaction = null;
		
		
		try {
			session = HibernateSessionUtil.getSessionFactory().getCurrentSession();
			transaction = session.beginTransaction();
			user = userDao.read(session, userId);
			Auditor auditor = new Auditor(new Date(), user.getLogin() , "LOGOUT");
			auditorDao.create(session, auditor);
			transaction.commit();
			
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
			transaction.rollback();
		}
		
	}

}
